package com.zjw.hyphenateui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.havi.bean.MessageBean;
import com.example.havi.helper.GlideHelper;
import com.example.havi.helper.MessageTypes;
import com.example.havi.util.LogUtil;
import com.hyphenate.chat.EMChatRoom;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.exceptions.HyphenateException;
import com.hyphenate.util.DateUtils;
import com.zjw.hyphenateui.R;
import com.zjw.hyphenateui.util.GsonUtils;
import com.zjw.hyphenateui.widget.easyadapter.BaseViewHolder;
import com.zjw.hyphenateui.widget.easyadapter.RecyclerArrayAdapter;

import java.util.Date;
import java.util.Map;

public class ConversationAdapter extends RecyclerArrayAdapter<EMConversation> {

    public ConversationAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.item_conversation, parent, false));
    }

    class ViewHolder extends BaseViewHolder<EMConversation> {
        TextView unread_msg_number_tv, name_tv, time_tv, mentioned_tv, message_tv, btnDelete;
        ImageView avatar_iv, msg_state_iv;
        View root;

        public ViewHolder(View itemView) {
            super(itemView);
            avatar_iv = itemView.findViewById(R.id.avatar);
            unread_msg_number_tv = itemView.findViewById(R.id.unread_msg_number);
            name_tv = itemView.findViewById(R.id.name);
            time_tv = itemView.findViewById(R.id.time);
            msg_state_iv = itemView.findViewById(R.id.msg_state);
            mentioned_tv = itemView.findViewById(R.id.mentioned);  //群聊显示有人@我 这里不使用
            message_tv = itemView.findViewById(R.id.message);
            btnDelete = itemView.findViewById(R.id.btnDelete);
            root = itemView.findViewById(R.id.root);
        }

        @Override
        public void setData(final EMConversation conversation) {
            updateUI_2(conversation);   //
            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EMClient.getInstance().chatManager().deleteConversation(conversation.conversationId(), false);
                    remove(conversation);
                }
            });

            root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (clickListener != null) {
                        clickListener.onItemClick(view, getDataPosition());
                    }
                }
            });
        }


        private void updateUI_2(EMConversation conversation) {
            EMMessage lastMessage = conversation.getLastMessage();
            String username = conversation.conversationId();
            //消息未读数
            if (conversation.getUnreadMsgCount() > 0) {
                unread_msg_number_tv.setText(String.valueOf(conversation.getUnreadMsgCount()));
                unread_msg_number_tv.setVisibility(View.VISIBLE);
            } else {
                unread_msg_number_tv.setVisibility(View.INVISIBLE);
            }

            //显示消息类容
            Map<String, Object> map = lastMessage.ext();
            MessageBean messageBean = MessageBean.mapToBean(map);

            if (messageBean == null) messageBean = new MessageBean();
            if (lastMessage.getChatType() == EMMessage.ChatType.GroupChat) {    //群聊会话
                GlideHelper.INSTANCE.loadCircleImage(avatar_iv, messageBean.getGroupAvatar() + "");
                name_tv.setText(messageBean.getGroupName() + "");
//                    EMGroup group = EMClient.getInstance().groupManager().getGroup(conversation.conversationId());  //获取群信息
            } else if (lastMessage.getChatType() == EMMessage.ChatType.ChatRoom) {    //聊天室会话
                avatar_iv.setImageResource(R.mipmap.ic_launcher);
                EMChatRoom room = EMClient.getInstance().chatroomManager().getChatRoom(conversation.conversationId());
                name_tv.setText(room != null && !TextUtils.isEmpty(room.getName()) ? room.getName() : conversation.conversationId());
            } else { //单聊会话
                if (lastMessage.direct() == EMMessage.Direct.RECEIVE) {
                    //会话中最后一条消息是接收的取avatar和nickName
                    GlideHelper.INSTANCE.loadCircleImage(avatar_iv, messageBean.getAvatar() + "");
                    name_tv.setText(messageBean.getNickName() + "");
                } else {
                    //会话中最后一条消息是自己发出去的 取toAvatar和toNickName
                    GlideHelper.INSTANCE.loadCircleImage(avatar_iv, messageBean.getToAvatar() + "");
                    name_tv.setText(messageBean.getToNickName() + "");
                }
            }
            String digest = "";
            switch (messageBean.getCustomMessageType()) {
                case MessageTypes.BASIC_MESSAGE:   //环信基础类型
                    switch (lastMessage.getType()) {
                        case LOCATION:
                            if (lastMessage.direct() == EMMessage.Direct.RECEIVE) {
                                digest = "[" + lastMessage.getFrom() + "的位置]";
                            } else {
                                digest = "[我的位置]";
                            }
                            break;
                        case IMAGE:
                            digest = "[图片]";
                            break;
                        case VOICE:
                            digest = "[语音]";
                            break;
                        case VIDEO:
                            digest = "[视频]";
                            break;
                        case TXT:
                            EMTextMessageBody txtBody = (EMTextMessageBody) lastMessage.getBody();
                            digest = txtBody.getMessage();
                            break;
                        case FILE:
                            digest = "[文件]";
                            break;
                        default:
                            Log.e("zjw", "error, unknow type");
                    }
                    break;
                case MessageTypes.CARD_MESSAGE:     //自定义名片类型
                    //跳转个人名片
                    if (lastMessage.direct() == EMMessage.Direct.RECEIVE) {
                        digest = "[收到一张名片]";
                    } else {
                        digest = "[发出一张名片]";
                    }
                    break;
            }


            message_tv.setText(digest);

            //时间
            time_tv.setText(DateUtils.getTimestampString(new Date(lastMessage.getMsgTime())));

            //消息状态
            if (lastMessage.direct() == EMMessage.Direct.SEND && lastMessage.status() == EMMessage.Status.FAIL) {
                msg_state_iv.setVisibility(View.VISIBLE);
            } else {
                msg_state_iv.setVisibility(View.GONE);
            }

        }

    }

    public interface OnItemRootClickListener {
        void onItemClick(View view, int position);
    }

    OnItemRootClickListener clickListener;

    public void setOnItemRootClickListener(OnItemRootClickListener clickListener) {
        this.clickListener = clickListener;
    }

}
